package com.sh.test_one.adapater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sh.test_one.R;
import com.sh.test_one.bean.ListItem;

import java.util.List;

public class ListItemAdapter extends BaseAdapter {


    private Context context;
    private List<ListItem> listItems;

    public ListItemAdapter(Context context, List<ListItem> listItems) {
        this.context = context;
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
            holder = new ViewHolder();
            holder.title = convertView.findViewById(R.id.title);
            holder.details = convertView.findViewById(R.id.details);
            holder.times = convertView.findViewById(R.id.times);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ListItem listItem = listItems.get(position);
        holder.title.setText(listItem.getTitle());
        holder.details.setText(listItem.getDetails());
        holder.times.setText(listItem.getTimes());

        return convertView;
    }



    private static class ViewHolder {
        TextView title;
        TextView details;
        TextView times;
    }

}